<?php

namespace App\Http\Controllers;

use App\Admin\Model\Gacss;
use App\Admin\Model\Product;
use App\Admin\Model\WaterCode;
use App\Admin\Model\Circulation;
use App\Admin\Model\Attribute;
use App\Admin\Model\Heading;
use App\Admin\Model\System;
use App\Admin\Model\Flora;
use App\Admin\Model\PdfUrl;
use Illuminate\Http\Request;

class VerifyController extends Controller
{
    /**
     * 13码查询接口
     * @param Request $request
     */
    public function verifyList (Request $request)
    {

        if(!isset($request->all()['code_list'])){
            return $this->apiReturn('','填写内容为空','false');
        }
        $code_list = $request->all()['code_list'];

        if($code_list=='588'){
            return $this->apiReturn(array('url'=>'http://code.guohlt.com/web/PDF/番茄.pdf'),'番茄pdf',2);
        }

        if($code_list=='6'){
            return $this->apiReturn('6666');
        }

        $verify_list = Gacss::where('code_list',$code_list)->get()->toArray();

        if(!isset($verify_list[0])){
            return $this->apiReturn('','条形码不存在','false');
        }

        //识别码
        $data['discern_name'] = Heading::where('id',$verify_list[0]['discern_id'])->value('name');

        //产品6位流水码
        $water_list = WaterCode::where('id',$verify_list[0]['water_id'])->get()->toArray();
        $data['water_code'] = $water_list[0]['code'];

        //获取产品信息
        $variety_list = System::where('id',$water_list[0]['variety_id'])->get()->toArray();
        //品种名
        $data['variety_name'] = $variety_list[0]['name'];
        //产品名
        $data['variety_product_name'] = $variety_list[0]['product_name'];
        //产品15码
        $data['variety_code'] = $variety_list[0]['code_list'];

        //获取界门纲目科属种
        $data['jie_name'] = Flora::where('id',$variety_list[0]['jie_id'])->value('name');
        $data['men_name'] = Flora::where('id',$variety_list[0]['men_id'])->value('name');
        $data['gang_name'] = Flora::where('id',$variety_list[0]['gang_id'])->value('name');
        $data['mu_name'] = Flora::where('id',$variety_list[0]['mu_id'])->value('name');
        $data['ke_name'] = Flora::where('id',$variety_list[0]['ke_id'])->value('name');
        $data['shu_name'] = Flora::where('id',$variety_list[0]['shu_id'])->value('name');
        $data['zhong_name'] = Flora::where('id',$variety_list[0]['zhong_id'])->value('name');

        //获取种的pdf链接
        $pdf = PdfUrl::where('zhong_id',$variety_list[0]['zhong_id'])->get()->toArray();

        if(isset($pdf[0])){
            $data['pdf_title'] = $pdf[0]['title'];
            $data['pdf_url'] = $pdf[0]['url'];
        }else{
            $data['pdf_title'] = '';
            $data['pdf_url'] = '';
        }


        //获取等级分类
        $data['grade_name'] = Product::where('id',$water_list[0]['grade_id'])->value('name');

        //获取产品分类
        $data['gc_class_name'] = Product::where('id',$water_list[0]['gc_class_id'])->value('name');

        //获取流通码
        $circulation_list = Circulation::where('id',$verify_list[0]['circulation_id'])->get()->toArray();
        //流水码
        $data['circulation_code'] = $circulation_list[0]['code'];
        //流通15码
        $data['circulation_code_list'] = $circulation_list[0]['code_list'];
        //仓储流通方式
        $data['temp_name'] = Attribute::where('id',$circulation_list[0]['temp_id'])->value('code_detail');
        //材积
        $data['volume_name'] = Attribute::where('id',$circulation_list[0]['volume_id'])->value('code_detail');
        //包装材质
        $data['pack_name'] = Attribute::where('id',$circulation_list[0]['pack_id'])->value('code_detail');
        //重量
        $data['spec_name'] = Attribute::where('id',$circulation_list[0]['spec_id'])->value('code_detail');

        return $this->apiReturn($data);
    }
}
